package com.example.lib;

import java.lang.annotation.Annotation;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

class TestMaine {



    public static void main(String[] args) {

        try{


            LinkedHashMap<String,String> linkedHashMap = new LinkedHashMap<>(10,0.75f,true);
            linkedHashMap.put("111","111");
            linkedHashMap.put("222","222");
            linkedHashMap.put("333","333");
            linkedHashMap.put("444","444");
            System.out.println(linkedHashMap.toString());
            linkedHashMap.get("222");
            linkedHashMap.get("111");
            System.out.println(linkedHashMap.toString());





            String str1 = new String("abc");
            String str2 = new String("abc");

            WeakReference<String> weakReference = new WeakReference<String>(str1);

//            System.gc();

//            while (true){
//                if(weakReference.get() == null){
//                    System.out.println(weakReference.get());
//                    break;
//                }else{
//                    System.out.println("睡眠");
//                    Thread.sleep(1000);
//                    System.gc();
//                }
//            }


//            IReqApi testMaine =create(IReqApi.class);
//            String result = testMaine.Login("熊巍","123456");
//            System.out.println(result);
        }catch (Exception e){
            System.out.println(e.toString());
        }
    }



    private void sortList(){

        List arrayList = new ArrayList();
        long startTime = System.currentTimeMillis();
        for(int i =0;i<10000000;i++){
            arrayList.add(i);
        }
        System.out.println("添加ArrayList时间差："+(System.currentTimeMillis() - startTime));

        List linkedList = new LinkedList();
        long linkedstartTime = System.currentTimeMillis();
        for(int i =0;i<10000000;i++){
            linkedList.add(i);
        }
        System.out.println("添加LinkedList时间差："+(System.currentTimeMillis() - linkedstartTime));

        long startTime1 = System.currentTimeMillis();
        for(int i = arrayList.size()-1;i>0;i--){
            arrayList.remove(i-1);
        }
        System.out.println("删除arrayList时间差："+(System.currentTimeMillis() - startTime1));

        long startTime2 = System.currentTimeMillis();
        for(int i = linkedList.size()-1;i>0;i--){
            linkedList.remove(i);
        }
        System.out.println("删除LinkedList时间差："+(System.currentTimeMillis() - startTime2));


    }

    public static <T> T create(Class<T> service){
        return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[]{service}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object... args) {
                ReqType reqType = method.getAnnotation(ReqType.class);
                System.out.println("请求类型："+reqType.reqType().toString());
                ReqUrl reqUrl = method.getAnnotation(ReqUrl.class);
                System.out.println("请求地址："+reqUrl.reqUrl());
                Type[] params = method.getGenericParameterTypes();
                Annotation[][] paramArray = method.getParameterAnnotations();
                for(int i = 0;i<paramArray.length;i++){
                    Annotation[] reqParam = paramArray[i];
                    if(reqParam!=null){
                        ReqParam param = (ReqParam) reqParam[0];
                        System.out.println("请求参数："+param.value()+"=="+args[i]);

                    }
                }



                return "请求成功";
            }
        });
    }

}
